<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>3-5</title>
    <style type="text/css">
        body{font:12px/1.5 arial;text-align:center;}
        .f-text{width:50px;border:1px solid #ccc;background:#f0f0f0;font-family:inherit;padding:3px;margin-right:10px;}
    </style>
</head>
<body>
<input class="f-text" type="text" />
<input value="是否为两位数" type="button" />
<script type="text/javascript">
    window.onload = function(){
        var inputs = document.getElementsByTagName("input");
        var span = document.getElementsByTagName("span")[0];

        var size = inputs.length;
        inputs[0].onkeyup = function(){
            this.value = this.value.replace(/[^\d]/,"");
        };
        inputs[1].onclick = function(){
            //(inputs[0].value.length == 2)?alert("是2位数"):alert("不是2位数");
            (inputs[0].value == "")?alert("不能为空"):
                (/^[0-9]{2}$/.test(inputs[0].value))?alert("是2位数"):alert("是"+inputs[0].value.length+"位数");
        };


    }
</script>
</body>
</html>